#pragma once
#include "cupid.h"

class MsgSn;
namespace cupid
{
class IMessagePort;
class NetMsg;
class User;
class WorldKernel 
{
public:
	WorldKernel(void);
	~WorldKernel(void);
public: // IWorld
	virtual bool	create(IMessagePort* port);
	virtual bool	processMsg(int idPacket, void* buf, int type, int from);
	virtual bool	onTimer(time_t curr);
	virtual bool	release();

protected: // ISocket
	virtual bool sendMsg			(NetMsg* pNetMsg);
	virtual bool sendMsg			(int idSocket, int idMsg, const char* pBuf, int nMsgLen);

	virtual bool closeSocket		(int idSocket);		

public: // send
	virtual bool sendClientMsg (int idSocket, NetMsg* msg);

	bool	processClientMsg(int idSocket, int idMsg, const char *buf, int size);

	User*	getUser(string name);
	User*	getUser(int id);
	User*	getUserBy(int sock);
	void	sendFriends(User* user);


public:
	void	onUserSignup(MsgSn* sn);
	void	onUserSignin(MsgSn* sn);
	void	onUserTalk(MsgSn* sn);
private:
	User*	newUser(int sock, string name, string password);
	int		getUserID(string name);
	int		getUserID(int sock);
	void	loadUsers();
	void	addUser(User* user);


private:
	int				m_idProcess;
	IMessagePort*	m_pMsgPort;
	map<int,User*>	m_userList;
	map<string,int>	m_userName2ID;
	map<int,int>	m_userSock2ID;
};
}